{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.9644758 ,  0.37379675, -0.86335931,  0.62906044, -0.29848509],\n",
       "       [-0.62025683, -0.28264341, -0.16873975,  0.17691796,  0.59186464],\n",
       "       [-0.09690524,  0.59846192,  0.1009573 , -0.02684819, -0.67823582],\n",
       "       [ 0.42667761, -0.66070169, -0.02103328, -0.25439922,  0.95823717],\n",
       "       [-0.91269148,  0.68655429, -0.38881965,  0.76019979,  0.80421292],\n",
       "       [-0.27613652, -0.81957316,  0.48129569, -0.33268588,  0.47658797]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change = np.random.uniform(-1, 1, (6, 5))\n",
    "stock_change"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataFrame的使用，类比于二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4\n",
       "0 -0.964476  0.373797 -0.863359  0.629060 -0.298485\n",
       "1 -0.620257 -0.282643 -0.168740  0.176918  0.591865\n",
       "2 -0.096905  0.598462  0.100957 -0.026848 -0.678236\n",
       "3  0.426678 -0.660702 -0.021033 -0.254399  0.958237\n",
       "4 -0.912691  0.686554 -0.388820  0.760200  0.804213\n",
       "5 -0.276137 -0.819573  0.481296 -0.332686  0.476588"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas.DataFrame(ndarray, index, columns, fre)\n",
    "stock_c = pd.DataFrame(stock_change)\n",
    "stock_c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 修改索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['股票1', '股票2', '股票3', '股票4', '股票5', '股票6']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改列索引\n",
    "stock_index = ['股票{}'.format(i+1) for i in range(stock_c.shape[0])]\n",
    "stock_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-05', '2020-08-06', '2020-08-07', '2020-08-10',\n",
       "               '2020-08-11'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改行索引\n",
    "stock_columns = pd.date_range('2020-8-5', periods=5, freq='B')  # 参数分别为start, end, periods, freq, end和periods人写一个就行，都能表示除最后的日期，freq指定为B时只计算工作日\n",
    "# 这里的日期只能为Y-M-D\n",
    "stock_columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2020-08-05</th>\n",
       "      <th>2020-08-06</th>\n",
       "      <th>2020-08-07</th>\n",
       "      <th>2020-08-10</th>\n",
       "      <th>2020-08-11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票1</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票6</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     2020-08-05  2020-08-06  2020-08-07  2020-08-10  2020-08-11\n",
       "股票1   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "股票2   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票3   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票4    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票5   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "股票6   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c = pd.DataFrame(stock_change, index=stock_index, columns=stock_columns)\n",
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 5)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['股票1', '股票2', '股票3', '股票4', '股票5', '股票6'], dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2020-08-05', '2020-08-06', '2020-08-07', '2020-08-10',\n",
       "               '2020-08-11'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.9644758 ,  0.37379675, -0.86335931,  0.62906044, -0.29848509],\n",
       "       [-0.62025683, -0.28264341, -0.16873975,  0.17691796,  0.59186464],\n",
       "       [-0.09690524,  0.59846192,  0.1009573 , -0.02684819, -0.67823582],\n",
       "       [ 0.42667761, -0.66070169, -0.02103328, -0.25439922,  0.95823717],\n",
       "       [-0.91269148,  0.68655429, -0.38881965,  0.76019979,  0.80421292],\n",
       "       [-0.27613652, -0.81957316,  0.48129569, -0.33268588,  0.47658797]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.values  # 数据为ndarray类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2020-08-05</th>\n",
       "      <th>2020-08-06</th>\n",
       "      <th>2020-08-07</th>\n",
       "      <th>2020-08-10</th>\n",
       "      <th>2020-08-11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票1</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     2020-08-05  2020-08-06  2020-08-07  2020-08-10  2020-08-11\n",
       "股票1   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "股票2   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票3   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票4    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票5   -0.912691    0.686554   -0.388820    0.760200    0.804213"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.head()  # 如果不写多少条，则默认显示前五条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2020-08-05</th>\n",
       "      <th>2020-08-06</th>\n",
       "      <th>2020-08-07</th>\n",
       "      <th>2020-08-10</th>\n",
       "      <th>2020-08-11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票2</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票3</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票4</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票5</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票6</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     2020-08-05  2020-08-06  2020-08-07  2020-08-10  2020-08-11\n",
       "股票2   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票3   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票4    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票5   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "股票6   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.tail()  # 如果不写条数，默认后五条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>股票1</th>\n",
       "      <th>股票2</th>\n",
       "      <th>股票3</th>\n",
       "      <th>股票4</th>\n",
       "      <th>股票5</th>\n",
       "      <th>股票6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.912691</td>\n",
       "      <td>-0.276137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-06</th>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.819573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-07</th>\n",
       "      <td>-0.863359</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.481296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-10</th>\n",
       "      <td>0.629060</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>-0.332686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-11</th>\n",
       "      <td>-0.298485</td>\n",
       "      <td>0.591865</td>\n",
       "      <td>-0.678236</td>\n",
       "      <td>0.958237</td>\n",
       "      <td>0.804213</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 股票1       股票2       股票3       股票4       股票5       股票6\n",
       "2020-08-05 -0.964476 -0.620257 -0.096905  0.426678 -0.912691 -0.276137\n",
       "2020-08-06  0.373797 -0.282643  0.598462 -0.660702  0.686554 -0.819573\n",
       "2020-08-07 -0.863359 -0.168740  0.100957 -0.021033 -0.388820  0.481296\n",
       "2020-08-10  0.629060  0.176918 -0.026848 -0.254399  0.760200 -0.332686\n",
       "2020-08-11 -0.298485  0.591865 -0.678236  0.958237  0.804213  0.476588"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.T  # 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['股票__0', '股票__1', '股票__2', '股票__3', '股票__4', '股票__5']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改索引，索引必须整行修改，不能之修改列或者行的单个索引\n",
    "new_stock_index = [\"股票__{}\".format(i) for i in range(stock_c.shape[0])]\n",
    "new_stock_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2020-08-05</th>\n",
       "      <th>2020-08-06</th>\n",
       "      <th>2020-08-07</th>\n",
       "      <th>2020-08-10</th>\n",
       "      <th>2020-08-11</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票__0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       2020-08-05  2020-08-06  2020-08-07  2020-08-10  2020-08-11\n",
       "股票__0   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "股票__1   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票__2   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票__3    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票__4   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "股票__5   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.index = new_stock_index\n",
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>物理</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>体育</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票__0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             数学        物理        英语        语文        体育\n",
       "股票__0 -0.964476  0.373797 -0.863359  0.629060 -0.298485\n",
       "股票__1 -0.620257 -0.282643 -0.168740  0.176918  0.591865\n",
       "股票__2 -0.096905  0.598462  0.100957 -0.026848 -0.678236\n",
       "股票__3  0.426678 -0.660702 -0.021033 -0.254399  0.958237\n",
       "股票__4 -0.912691  0.686554 -0.388820  0.760200  0.804213\n",
       "股票__5 -0.276137 -0.819573  0.481296 -0.332686  0.476588"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_stock_columns = ['数学', '物理', '英语', '语文', '体育']\n",
    "stock_c.columns = new_stock_columns\n",
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1995-05-29', '1995-05-30', '1995-05-31', '1995-06-01',\n",
       "               '1995-06-02'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_date = pd.date_range('1995-5-27', periods=5, freq='B')\n",
    "new_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c.columns = new_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1995-05-29</th>\n",
       "      <th>1995-05-30</th>\n",
       "      <th>1995-05-31</th>\n",
       "      <th>1995-06-01</th>\n",
       "      <th>1995-06-02</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票__0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       1995-05-29  1995-05-30  1995-05-31  1995-06-01  1995-06-02\n",
       "股票__0   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "股票__1   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票__2   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票__3    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票__4   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "股票__5   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# stock_c.columns[2] = 'hah'  # 不能修改单个索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 重设索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>1995-05-29 00:00:00</th>\n",
       "      <th>1995-05-30 00:00:00</th>\n",
       "      <th>1995-05-31 00:00:00</th>\n",
       "      <th>1995-06-01 00:00:00</th>\n",
       "      <th>1995-06-02 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>股票__0</td>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>股票__1</td>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>股票__2</td>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>股票__3</td>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>股票__4</td>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>股票__5</td>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index  1995-05-29 00:00:00  1995-05-30 00:00:00  1995-05-31 00:00:00  \\\n",
       "0  股票__0            -0.964476             0.373797            -0.863359   \n",
       "1  股票__1            -0.620257            -0.282643            -0.168740   \n",
       "2  股票__2            -0.096905             0.598462             0.100957   \n",
       "3  股票__3             0.426678            -0.660702            -0.021033   \n",
       "4  股票__4            -0.912691             0.686554            -0.388820   \n",
       "5  股票__5            -0.276137            -0.819573             0.481296   \n",
       "\n",
       "   1995-06-01 00:00:00  1995-06-02 00:00:00  \n",
       "0             0.629060            -0.298485  \n",
       "1             0.176918             0.591865  \n",
       "2            -0.026848            -0.678236  \n",
       "3            -0.254399             0.958237  \n",
       "4             0.760200             0.804213  \n",
       "5            -0.332686             0.476588  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重设索引DataFrame对象.reset_index(drop=False)，drop默认是False，表示不删除原来索引\n",
    "stock_c.reset_index()  # 创建了新对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1995-05-29</th>\n",
       "      <th>1995-05-30</th>\n",
       "      <th>1995-05-31</th>\n",
       "      <th>1995-06-01</th>\n",
       "      <th>1995-06-02</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   1995-05-29  1995-05-30  1995-05-31  1995-06-01  1995-06-02\n",
       "0   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "1   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "2   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "3    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "4   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "5   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.reset_index(drop=True)  # 创建了新对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1995-05-29</th>\n",
       "      <th>1995-05-30</th>\n",
       "      <th>1995-05-31</th>\n",
       "      <th>1995-06-01</th>\n",
       "      <th>1995-06-02</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>股票__0</th>\n",
       "      <td>-0.964476</td>\n",
       "      <td>0.373797</td>\n",
       "      <td>-0.863359</td>\n",
       "      <td>0.629060</td>\n",
       "      <td>-0.298485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__1</th>\n",
       "      <td>-0.620257</td>\n",
       "      <td>-0.282643</td>\n",
       "      <td>-0.168740</td>\n",
       "      <td>0.176918</td>\n",
       "      <td>0.591865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__2</th>\n",
       "      <td>-0.096905</td>\n",
       "      <td>0.598462</td>\n",
       "      <td>0.100957</td>\n",
       "      <td>-0.026848</td>\n",
       "      <td>-0.678236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__3</th>\n",
       "      <td>0.426678</td>\n",
       "      <td>-0.660702</td>\n",
       "      <td>-0.021033</td>\n",
       "      <td>-0.254399</td>\n",
       "      <td>0.958237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__4</th>\n",
       "      <td>-0.912691</td>\n",
       "      <td>0.686554</td>\n",
       "      <td>-0.388820</td>\n",
       "      <td>0.760200</td>\n",
       "      <td>0.804213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>股票__5</th>\n",
       "      <td>-0.276137</td>\n",
       "      <td>-0.819573</td>\n",
       "      <td>0.481296</td>\n",
       "      <td>-0.332686</td>\n",
       "      <td>0.476588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       1995-05-29  1995-05-30  1995-05-31  1995-06-01  1995-06-02\n",
       "股票__0   -0.964476    0.373797   -0.863359    0.629060   -0.298485\n",
       "股票__1   -0.620257   -0.282643   -0.168740    0.176918    0.591865\n",
       "股票__2   -0.096905    0.598462    0.100957   -0.026848   -0.678236\n",
       "股票__3    0.426678   -0.660702   -0.021033   -0.254399    0.958237\n",
       "股票__4   -0.912691    0.686554   -0.388820    0.760200    0.804213\n",
       "股票__5   -0.276137   -0.819573    0.481296   -0.332686    0.476588"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 以某列值为新的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month</th>\n",
       "      <th>year</th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2012</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>2014</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>2013</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>2014</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   month  year  sale\n",
       "0      1  2012    55\n",
       "1      4  2014    40\n",
       "2      7  2013    84\n",
       "3     10  2014    31"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'month': [1, 4, 7, 10],\n",
    "                    'year': [2012, 2014, 2013, 2014],\n",
    "                    'sale':[55, 40, 84, 31]})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2012</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2014</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2013</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2014</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year  sale\n",
       "month            \n",
       "1      2012    55\n",
       "4      2014    40\n",
       "7      2013    84\n",
       "10     2014    31"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对象.set_index(keys, drop=True)  # drop默认为True，表示删除原来的列索引\n",
    "df.set_index('month')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MultiIndex和Panel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MultiIndex，类比于三维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>2012</th>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <th>2014</th>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <th>2013</th>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <th>2014</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            sale\n",
       "month year      \n",
       "1     2012    55\n",
       "4     2014    40\n",
       "7     2013    84\n",
       "10    2014    31"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.set_index(['month', 'year'])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>sale</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>month</th>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>2012</th>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <th>2014</th>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <th>2013</th>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <th>2014</th>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            sale\n",
       "month year      \n",
       "1     2012    55\n",
       "4     2014    40\n",
       "7     2013    84\n",
       "10    2014    31"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# MultiIndex的创建1\n",
    "df = df.set_index(['month', 'year'])  # 这样df就相当于是MultiIndex类型（三维数组）\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([( 1, 2012),\n",
       "            ( 4, 2014),\n",
       "            ( 7, 2013),\n",
       "            (10, 2014)],\n",
       "           names=['month', 'year'])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FrozenList(['month', 'year'])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FrozenList([[1, 4, 7, 10], [2012, 2013, 2014]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.levels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex([(1,  'red'),\n",
       "            (1, 'blue'),\n",
       "            (2,  'red'),\n",
       "            (2, 'blur')],\n",
       "           names=['number', 'color'])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# MultiIndex的创建2\n",
    "array = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blur']]\n",
    "new_mul = pd.MultiIndex.from_arrays(array, names=('number', 'color'))\n",
    "new_mul"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Series，类比于一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    4\n",
       "5    5\n",
       "6    6\n",
       "7    7\n",
       "8    8\n",
       "9    9\n",
       "dtype: int32"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建方式1\n",
    "a = pd.Series(np.arange(10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建方式2\n",
    "b = pd.Series([1, 2, 3], index=['a', 'b', 'c'])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "red       1\n",
       "blue      2\n",
       "yellow    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建方式3\n",
    "c = pd.Series({'red': 1, 'blue': 2, 'yellow': 3})\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['red', 'blue', 'yellow'], dtype='object')"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3], dtype=int64)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c['yellow']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
